package com.example.studentmanager.Controller;

import com.example.studentmanager.Pojo.Student;
import com.example.studentmanager.Service.impl.Studentimpl;
import com.example.studentmanager.Util.Dbtool;
import com.mysql.cj.Session;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "upstu",value = "/upstu")
public  class UpdateStu extends HttpServlet {
    private  String mss="数据更新成功";
    private  String error="数据更新失败";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        res.setCharacterEncoding("utf-8");
        String id=    req.getParameter("studentid");
        Studentimpl ss=new Studentimpl();
        List<Student> list= ss.queryByKey(id);
        if (list.isEmpty()){
            req.setAttribute("error",error);
            RequestDispatcher ds= req.getRequestDispatcher("./404.jsp");
            ds.forward(req,res);
        }else {
            Student s=new Student();
            s.setStudentid(Integer.parseInt(req.getParameter("studentid")));
            s.setName(req.getParameter("name"));
            s.setCls(req.getParameter("cls"));
            s.setPhone(Integer.parseInt(req.getParameter("phone")));
            s.setGrade(req.getParameter("grade"));
            s.setHome(req.getParameter("home"));
            s.setState(req.getParameter("state"));
            System.out.println(s.getStudentid()+s.getName());
            Studentimpl sss=new Studentimpl();
            int r= sss.Update(s);

            if (r>0){
HttpSession  session = req.getSession();
String admin="admin";
String password="123456";
session.setAttribute("user",admin);
session.setAttribute("password",password);

                req.getRequestDispatcher("main").forward(req,res);
            }else {
                req.setAttribute("error",error);
                RequestDispatcher ds= req.getRequestDispatcher("./404.jsp");
                ds.forward(req,res);
            }
        }
    }
    }
